<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2024/4/23
 * Time: 14:20
 */

namespace app\controller\index\user;


use app\controller\index\BaseController;
use app\exception\IndexException;
use app\logic\index\user\UserSpreadLogic;
use support\Request;
use support\Response;

class SpreadController extends BaseController
{
    public function getList(Request $request):Response
    {
        $param = [];
        $param['novel_sn'] = $request->input('novel_sn');
        $param['status'] = $request->input('status');
        if(empty($param['novel_sn'])){
            throw new IndexException(4020,'小说不存在');
        }
        $data = UserSpreadLogic::getInstance()->getList($param);
        return $this->endSuccess($data);

    }

    public function submitKeyword(Request $request):Response
    {
        $param = [];
        $param['keyword'] = $request->input('keyword');
        $param['novel_sn'] = $request->input('novel_sn');
        if(empty($param['novel_sn'])){
            throw new IndexException(4020,'小说不存在');
        }
        if(empty($param['keyword'])){
            throw new IndexException(4020,'关键词必填');
        }
        $data = UserSpreadLogic::getInstance()->submitKeyword($param);
        return $this->endSuccess($data);

    }

    public function submitWorkLink(Request $request):Response
    {
        $param = [];
        $param['keyword_id'] = $request->input('keyword_id');
        $param['work_link_content'] = $request->input('work_link_content');
        $param['platform_account_id'] = $request->input('platform_account_id');
        if(empty($param['keyword_id'])){
            throw new IndexException(4020,'关键词不存在');
        }
        if(empty($param['work_link_content'])){
            throw new IndexException(4020,'作品链接必填');
        }
        if(empty($param['platform_account_id'])){
            throw new IndexException(4020,'平台账号必选');
        }
        $data = UserSpreadLogic::getInstance()->submitWorkLink($param);
        return $this->endSuccess($data);

    }

    public function deleteKeyword(Request $request):Response
    {
        $param = [];
        $param['keyword_id'] = $request->input('keyword_id');
        if(empty($param['keyword_id'])){
            throw new IndexException(4020,'关键词不存在');
        }
        $data = UserSpreadLogic::getInstance()->deleteKeyword($param);
        return $this->endSuccess($data);
    }


    /**
     * 获取关键词历史记录
     * @param Request $request
     * @return Response
     * @throws IndexException
     */
    public function getUserHistoryKeyWordLink(Request $request): Response
    {
        $param = [];
        $param['keyword_id'] = $request->input('keyword_id');
        if(empty($param['keyword_id'])){
            throw new IndexException(4020,'关键词不存在');
        }
        $data = UserSpreadLogic::getInstance()->getUserHistoryKeyWordLink($param);

        return $this->endSuccess($data);
    }

    /**
     * 删除关键词历史记录
     * @param Request $request
     * @return Response
     * @throws IndexException
     */
    public function delUserHistoryKeyWordLink(Request $request): Response
    {
        $param = [];
        $param['keyword_id'] = $request->input('keyword_id');
        $param['work_link_ids'] = $request->input('work_link_ids');
        if(empty($param['keyword_id'])){
            throw new IndexException(4020,'关键词不存在');
        }
        if(empty($param['work_link_ids'])){
            throw new IndexException(4020,'请选择要删除的历史');
        }
        $data = UserSpreadLogic::getInstance()->delUserHistoryKeyWordLink($param);
        return $this->endSuccess($data);
    }
}